package com.dotflat.docs.controller;

import com.dotflat.docs.domain.Document;
import com.dotflat.docs.po.DocumentPO;
import com.dotflat.docs.service.DocumentService;
import com.dotflat.rest.controller.RestfulBaseController;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/api/documents")
@Slf4j
@Api(tags = "文档资料 - 条目")
public class APIDocController extends RestfulBaseController<Document, DocumentService, DocumentPO> {

    @Autowired
    public void setBaseService(DocumentService service){
        super.setBaseService(service);
    }

    @ApiOperation(value = "增加一条记录")
    @PostMapping()
    @Override
    public StatusResponse add(@RequestHeader(TICKET) String ticket, DocumentPO param){
        StaffDTO staff = cache.getStaff(ticket);

        param.setStaffName(staff.getRealName());
        param.setStaffHeadImg(staff.getHeadImgUrl());

        Document ret = baseService.add(staff.getAreaId(), staff.getId(), param);

        List<Document> tmp = new ArrayList<>();
        tmp.add(ret);

        return StatusResponse.success(tmp);
    }

}
